package com.rs.controller;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.rs.config.CurrentUser;
import com.rs.entity.Class;
import com.rs.entity.Response;
import com.rs.entity.Student;
import com.rs.entity.excel.StudentExcelEntity;
import com.rs.entity.login.Menu;
import com.rs.entity.login.Role;
import com.rs.entity.login.User;
import com.rs.service.ClassService;
import com.rs.service.RoleService;
import com.rs.service.StudentService;
import com.rs.service.TermService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @author dehang
 * @date 2019/06/01
 */
@Controller
@RequestMapping("class")
public class ClassController {

    @Autowired
    private ClassService classService;

    @Autowired
    private StudentService studentService;

    @RequestMapping("/index")
    public String index(Model model) {
        return "class/index";
    }

    @RequestMapping("/list")
    @ResponseBody
    public Response list(long page, long limit,String name) {
        List<Class> classes = classService.getClassList(page,limit,name);
        return Response.list(classes,classService.getClassSum(name));
    }

    @RequestMapping("/add")
    @ResponseBody
    public long addClass(String name, String remark) {
        Integer id=CurrentUser.getUserId();
        return classService.addClass(name, remark,id);
    }

    @RequestMapping("/update")
    @ResponseBody
    public void updateClass(Long id, String name, String remark) {

        classService.updateClass(id, name, remark);
    }

    @RequestMapping("/del")
    @ResponseBody
    public void delClass(long id) {

        classService.delClass(id);
    }

    @RequestMapping("/allocation")
    public String allocation(Model model, long id) {

        //已有学生
        List<Student> students=classService.getHaveStudent(id);
        model.addAttribute("two", students);
        model.addAttribute("id", id);
        return "class/allocation";
    }

    @RequestMapping("/allot")
    @ResponseBody
    public String allot(String mid,int id){
        classService.allot(mid,id);
        return "success";
    }


    @RequestMapping("/getStudent")
    public String getStudent(Model model) {
        Integer id=CurrentUser.getUserId();
        //查询该老师的班级
        List<Class> classes=classService.getClassByTeaId(id);
        for (Class c:classes){
            //根据班级查学生
            List<Student> students=classService.getHaveStudent(c.getId());
            c.setStudentList(students);
        }
        model.addAttribute("classes", classes);
        return "index/allocation";
    }


    @RequestMapping("/uploadStudents")
    @ResponseBody
    public Response<Boolean> test(@RequestParam("file") MultipartFile multipartFile, @RequestParam("termId") Integer cid) throws Exception {
        ImportParams params = new ImportParams();
        List<StudentExcelEntity> list = ExcelImportUtil.importExcel(multipartFile.getInputStream(), StudentExcelEntity.class, params);
        studentService.uploadSaveByClass(list, cid);
        return Response.success(Boolean.TRUE);
    }

    @RequestMapping("/getClasskc")
    @ResponseBody
    public List<Class> getClasskc() {
        Integer id=CurrentUser.getUserId();
        List<Class> classes = classService.getUserClassList(id);
        return classes;
    }



}
